import os
from pathlib import Path
from cocotb_test.simulator import run

def start():
    project_root = Path(__file__).parent.resolve()
    sim_build = project_root / "sim_build"
    os.makedirs(sim_build, exist_ok=True)
    # 强制设置运行参数环境变量
    os.environ["VERILATOR_FLAGS"] = "--trace --trace-fst"
    # 环境变量设置 - 关键部分
    os.environ["COCOTB_RESOLVE_X"] = "ZEROS"
    os.environ["COCOTB_SIM"] = "1"
    os.environ["COCOTB_WAVES"] = "1"
    # Verilator 特定设置
    os.environ["VERILATOR_TRACE"] = "1"  # 启用跟踪
    os.environ["VERILATOR_TRACE_FMT"] = "fst"  # 指定格式为 fst
    
    run(
        verilog_sources=[str(project_root / "rtl" / "counter.sv")],
        toplevel="counter",
        module="tests.tb_top",
        python_search=[str(project_root / "tests")],
        includes=[str(project_root / "rtl")],
        simulator="verilator",
        extra_args=[ 
            "--coverage",
            "-CFLAGS", f"-I{str(project_root / 'tests')}",
        ],
        sim_args=["--trace", 
            "--trace-fst", 
        ],
        sim_build=str(sim_build),
        force_compile=True,
        waves=True,  # 关键：强制启用波形
        gui=False, # 添加关键的 GUI 参数 - 强制生成波形
    )

if __name__ == "__main__":
    start()